/**
 * 对公众平台发送给公众账号的消息加解密示例代码.
 * 
 * @copyright Copyright (c) 1998-2014 Tencent Inc.
 */

// ------------------------------------------------------------------------

package com.pet.server.ext.util.meituan;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.pet.server.ext.util.handler.BeanListJsonHander;


/**
 * XMLParse class
 *
 * 提供提取消息格式中的密文及生成回复消息格式的接口.
 */
public class XMLParse {

	
	/**
	 * 生成xml消息
	 * @param encrypt 加密后的消息密文
	 * @param signature 安全签名
	 * @param timestamp 时间戳
	 * @param nonce 随机字符串
	 * @return 生成的xml字符串
	 */
	public static String generate(String encrypt, String signature, String timestamp, String nonce) {

		String format = "<xml>\n" + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n"
				+ "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n"
				+ "<TimeStamp>%3$s</TimeStamp>\n" + "<Nonce><![CDATA[%4$s]]></Nonce>\n" + "</xml>";
		return String.format(format, encrypt, signature, timestamp, nonce);

	}
	public static List<XmlData> parseXmlToList(String xmltext){
		List<XmlData> l = new ArrayList<XmlData>();
		
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			StringReader sr = new StringReader(xmltext);
			InputSource is = new InputSource(sr);
			Document document = db.parse(is);
			Element root = document.getDocumentElement();
			Element deals = (Element)root.getElementsByTagName("deals").item(0);
			NodeList list = deals.getElementsByTagName("data");
			
			
			
			
			for(int i = 0; i < list.getLength(); i++){
			   Map<Object, Object> map = new HashMap<Object, Object>();
			   Element data=(Element)list.item(i);
			   Node deal = data.getElementsByTagName("deal").item(0);
			   //data.getel
			   Element shops = (Element)data.getElementsByTagName("shops").item(0);
			   Node shop = shops.getElementsByTagName("shop").item(0);

			   
			   NodeList nlist = deal.getChildNodes();
			   for(int j = 0; j < nlist.getLength(); j++){
				   Node node = nlist.item(j);
				   if(node.getNodeType()==1){
					   map.put(node.getNodeName().toLowerCase(), node.getTextContent());
				       //System.out.println(node.getNodeName().toLowerCase());
				   }
				}
			 
			   
			   NodeList slist = shop.getChildNodes();
			   //System.out.println(shop.getNodeName()+" ===========================");
			   
			   for(int k=0;k<slist.getLength();k++){
				   Node node = slist.item(k);
				   if(node.getNodeType()==1){
					   //System.out.println(node.getNodeName().toLowerCase());
					   map.put(node.getNodeName().toLowerCase(), node.getTextContent());
				   }
			   }
			   
			   BeanListJsonHander<XmlData> cr = new BeanListJsonHander<XmlData>(XmlData.class);
			   l.add(cr.handle(map, XmlData.class));
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return l;
	}
	public static XmlData parseXmlData(String src){
		BeanListJsonHander<XmlData> cr = new BeanListJsonHander<XmlData>(XmlData.class);
		Map<Object, Object> map =null;// parseXmlToList(src);
		return cr.handle(map, XmlData.class);
	}
	
	
	
	public static void main(String[] args){
		//String xmltext = XMLParse.generate("我的", "luoyouhua", "1342321212", "d");
		//System.out.println(xmltext);
		//XmlData data = parseXmlData(xmltext);
		//System.out.println(data.encrypt);
		//String strs = MeiTuanApi.callMeiTuanByCity("北京", "海淀区", "宠物", 10, "宠物", 0);
		String str = MeiTuanApi.callMeiTuanByDili(40.013232,116.45868,  "宠物", 10, "宠物", 0, 10);
		List<XmlData> l = parseXmlToList(str);
		for(XmlData d:l){
			//System.out.println(d.deal_name+"  "+d.value+"  "+d.price+"  "+d.deal_lat+" "+d.deal_img);
		}
		//System.out.println(MeiTuanApi.callMeiTuan());
	}
}
